package com.liziyi0914.morearmor.effect;

import com.liziyi0914.morearmor.PlayerManager;
import com.liziyi0914.morearmor.Utils;
import com.liziyi0914.morearmor.event.Listener;
import com.liziyi0914.morearmor.event.entity.EntityDamageEvent;
import com.liziyi0914.morearmor.event.entity.WorldTickEvent;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Items;
import net.minecraft.server.network.ServerPlayerEntity;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.function.Predicate;

/**
 * 2021/6/29<br>
 * MoreArmor<br>
 *
 * @author huanmeng_qwq
 */
public class NetherRackEffect extends AbstractEffect {
    private final Map<UUID, Boolean> lastEnables = new HashMap<>();

    @Listener
    public void onTick(WorldTickEvent e) {
        for (Entity entity : e.getWorld().getEntities()) {
            if (entity instanceof PlayerEntity) {
                final ServerPlayerEntity player = PlayerManager.get(entity.getUuid());
                if (player != null && lastEnables.getOrDefault(player.getUuid(), false)) {
                    if (!predicateArmor().test(player)) {
                        lastEnables.remove(player.getUuid());
                        return;
                    } else {
                        player.setFireTicks(20);
                    }
                }
            }
        }
    }

    @Listener
    public void onDamage(EntityDamageEvent e) {
        if (!e.getSource().isFire()) {
            return;
        }
        if (e.getSource().getName().equals(DamageSource.HOT_FLOOR.name)) return;
        if (!predicateArmor().test(e.getEntity())) return;
        lastEnables.put(e.getEntity().getUuid(), true);
    }

    @Override
    public Predicate<LivingEntity> predicateArmor() {
        return Utils.hasItemArmor(Items.NETHERRACK);
    }
}
